package sk.fiit.vppj.eventmanager.gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import sk.fiit.vppj.eventmanager.core.ConfigManager;
import sk.fiit.vppj.eventmanager.core.UserManager;
import sk.fiit.vppj.eventmanager.gui.administrator.AdministratorView;
import sk.fiit.vppj.eventmanager.gui.judge.JudgeView;
import sk.fiit.vppj.eventmanager.volunteer.VolunteerView;

/**
 * Main graphic class.
 * 
 * @author Marek
 * 
 */
public final class MainFrame extends JFrame {
	private static MainFrame mainFrame = new MainFrame();
	private JPanel topPanel = null;
	private JPanel bodyPanel = null;
	private static final int SCREEN_WIDTH = 1024;
	private final static Logger log = Logger.getLogger( MainFrame.class.getName() );

	private MainFrame() {
		init();
	}

	private void init() {
		try {
			String theme = new ConfigManager().getProperties().getProperty( "theme", "metal" );
			if( theme == null ) {
				UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
			}
			else if( theme.equals( "windows" ) ) {
				UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
			}
			else if( theme.equals( "nimbus" ) ) {
				UIManager.setLookAndFeel( "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" );
			}
			else if( theme.equals( "metal" ) ) {
				UIManager.setLookAndFeel( "javax.swing.plaf.metal.MetalLookAndFeel" );
			}
			else {

				UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
			}
		}
		catch ( ClassNotFoundException e ) {
			log.log( Level.INFO, "Invalid string which loads look and feel!" );
			e.printStackTrace();
		}
		catch ( InstantiationException e ) {
			log.log( Level.INFO, "Invalid string which loads look and feel!" );
			e.printStackTrace();
		}
		catch ( IllegalAccessException e ) {
			log.log( Level.INFO, "Invalid access to field!" );
			e.printStackTrace();
		}
		catch ( UnsupportedLookAndFeelException e ) {
			log.log( Level.INFO, "Look and feel is not installed on machine!" );
			e.printStackTrace();
		}
		setLayout( new BoxLayout( getContentPane(), BoxLayout.PAGE_AXIS ) );
		setMinimumSize( new Dimension( SCREEN_WIDTH, 640 ) );
		setDefaultCloseOperation( EXIT_ON_CLOSE );

		this.topPanel = new JPanel( new FlowLayout( FlowLayout.LEFT ) );
		this.topPanel.setMaximumSize( new Dimension( SCREEN_WIDTH, 90 ) );
		this.topPanel.setVisible( true );
		getContentPane().add( this.topPanel );
		showToolbar();

		showBody();
		setJMenuBar( getMenu() );

	}

	public static MainFrame getInstance() {
		return mainFrame;
	}

	public JPanel getTopPanel() {
		return this.topPanel;
	}

	public JPanel getBodyPanel() {
		return this.bodyPanel;
	}

	public void showToolbar() {

		this.topPanel.removeAll();
		this.topPanel.repaint();
		this.topPanel.setVisible( false );

		if( UserManager.getInstance().isLogIn() ) {
			showLogoutToolbar();
		}
		else {
			showLoginToolbar();
		}

		this.topPanel.setVisible( true );
	}

	/**
	 * Login toolbar - shows fields for credentials
	 */
	private void showLoginToolbar() {

		JLabel usernameLabel = new JLabel( "Username: " );
		topPanel.add( usernameLabel );
		final JTextField username = new JTextField( "admin", 10 );
		topPanel.add( username );
		JLabel passLabel = new JLabel( "Password: " );
		topPanel.add( passLabel );
		final JPasswordField pass = new JPasswordField( "hopeless21", 10 );
		topPanel.add( pass );
		JButton LoginButton = new JButton( "Login" );
		topPanel.add( LoginButton );
		LoginButton.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				UserManager.getInstance().login( username.getText(), String.valueOf( pass.getPassword() ) );
				showToolbar();
				showBody();
			}
		} );

	}

	private void showLogoutToolbar() {
		JLabel label = new JLabel( "Welcome, " + UserManager.getInstance().getUser().getName() );
		this.topPanel.add( label );
		JButton LogoutButton = new JButton( "Logout" );
		this.topPanel.add( LogoutButton );
		LogoutButton.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				UserManager.getInstance().logout();
				showToolbar();
				showBody();
			}
		} );
	}

	private void setBodyPanel(JPanel newPanel) {

		if( this.bodyPanel != null ) {
			this.bodyPanel.removeAll();
			this.bodyPanel.repaint();
			this.bodyPanel.setVisible( false );
		}

		if( newPanel == null ) {
			this.bodyPanel = new JPanel( new FlowLayout( FlowLayout.CENTER ) );
		}
		else {

			this.bodyPanel = newPanel;
		}

		getContentPane().add( this.bodyPanel );
		this.bodyPanel.setVisible( true );
	}

	private void showBody() {

		if( UserManager.getInstance().isLogIn() ) {
			if( UserManager.getInstance().getUser().getUsertype().equals( "Manager" )
					|| UserManager.getInstance().getUser().getUsertype().equals( "Administrator" )
					|| UserManager.getInstance().getUser().getUsertype().equals( "Super Administrator" ) ) {
				setBodyPanel( new AdministratorView() );
			}
			else if( UserManager.getInstance().getUser().getClazz().equals( "judge" ) ) {
				setBodyPanel( new JudgeView() );
			}
			else if( UserManager.getInstance().getUser().getClazz().equals( "volunteer" )
					&& UserManager.getInstance().getUser().getUsertype().equals( "Registered" ) ) {
				setBodyPanel( new VolunteerView() );
			}
		}
		else {
			setBodyPanel( null );
		}

	}

	private JMenuBar getMenu() {
		// creating menubar
		JMenuBar menuBar = new JMenuBar();
		JMenu menu = new JMenu( "File" );
		menuBar.add( menu );

		// exit option in menu
		JMenuItem subMenu = new JMenuItem( "Settings" );
		subMenu.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				showSettings();
			}
		} );
		menu.add( subMenu );

		JMenuItem subMenu1 = new JMenuItem( "Exit" );
		subMenu1.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		} );
		menu.add( subMenu1 );

		return menuBar;
	}

	private void showSettings() {
		final Settings settings = new Settings();
		mainFrame.setEnabled( false );
		settings.addWindowListener( new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent e) {
				super.windowClosed( e );
				mainFrame.setEnabled( true );
			}
		} );
	}
}
